package com.trip.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trip.common.dto.LoginDto;
import com.trip.common.dto.RegDto;
import com.trip.common.entity.TripUser;
import com.trip.common.util.PasswordEncode;
import com.trip.common.util.RedisUtil;
import com.trip.common.util.SendEmailAuthUtil;
import com.trip.common.util.TokenUtil;
import com.trip.common.vo.DataDictionary;
import com.trip.common.vo.MyResultMap;
import com.trip.common.vo.Result;
import com.trip.user.mapper.SysUserMapper;
import com.trip.user.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author hr
 * @since 2024-09-05
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, TripUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SendEmailAuthUtil sendEmailAuthUtil;
    @Autowired
    private PasswordEncode passwordEncode;


    @Override
    public Result loginByAccount(LoginDto loginDto) {
        TripUser user = sysUserMapper.loginByAccount(loginDto);
        if (!ObjectUtils.isEmpty(user)){
//            passwordEncode.matches(user.getPassword(),loginDto.getPassword())
            if (user.getPassword().equals(loginDto.getPassword())){
                return setToken(user);
            }else{
                return Result.error(DataDictionary.FAIL,"密码错误");
            }
        }else {
            return Result.error(DataDictionary.FAIL,"该用户不存在");
        }
    }

    @Override
    public Result loginByEmail(LoginDto loginDto) {
        //判断当前用户是否已注册
        TripUser user = sysUserMapper.loginByEmail(loginDto);
        if (ObjectUtils.isEmpty(user)){
            return Result.error(DataDictionary.NO_REG,"当前用户未注册");
        }


        //判断验证码是否正确
        int code = sendEmailAuthUtil.checkCode(loginDto);


        //验证码过期或不存在
        if (code == 0){
            return Result.error(DataDictionary.NO_DATA,"验证码已过期");
        }else if (code == 1){
            //校验通过
            return setToken(user);
        }else if (code == 2){
            //验证码错误
            return Result.error(DataDictionary.FAIL,"验证码错误");
        }else {
            return Result.error(DataDictionary.FAIL,"");
        }
    }

    @Override
    public void sendEmail(String email) {
        sendEmailAuthUtil.send(email);
    }

    @Override
    public Result checkIsRegisterByUserName(RegDto regDto) {
        TripUser i = sysUserMapper.checkIsRegisterByUserName(regDto);
        if (i != null){
            return Result.error("已被注册");
        }else {
            return Result.ok("未注册");
        }
    }

    @Override
    public Result checkIsRegisterByPhone(RegDto regDto) {
        TripUser i = sysUserMapper.checkIsRegisterByPhone(regDto);
        if (i != null){
            return Result.error("已被注册");
        }else {
            return Result.ok("未注册");
        }
    }

    @Override
    public Result checkIsRegisterByEmail(RegDto regDto) {
        TripUser i = sysUserMapper.checkIsRegisterByEmail(regDto);
        if (i != null){
            return Result.error("已被注册");
        }else {
            return Result.ok("未注册");
        }
    }


    @Override
    public Result register(RegDto regDto) {
        String encode = passwordEncode.encode(regDto.getPassword());
        regDto.setSex(regDto.getSex().equals("男")?"0":"1");
        regDto.setPassword(encode);
        int register = sysUserMapper.register(regDto);
        if (register > 0){
            return Result.ok("注册成功");
        }else {
            return Result.ok("注册失败");
        }
    }


    private Result setToken(TripUser user){
        String token = tokenUtil.createToken(String.valueOf(user.getUserId()),user.getUsername(),"user");
        redisUtil.set(token,user.getUsername());

        //登录成功半个小时后失效
        redisUtil.setExpire(token,60*30);
        HashMap<String, Object> myResultMap = MyResultMap.getMyResultMap();
        myResultMap.put("userName",user.getUsername());
        myResultMap.put("userId",user.getUserId());
        myResultMap.put("avatar",user.getAvatar());
        myResultMap.put("token",token);
        return Result.ok("登录成功，页面跳转中",myResultMap);
    }
}
